﻿namespace Joy.Erp.Ufida.Core
{
    public static class U9ApiExtensions
    {
        public static int GetParentCodeLength(this string code, string treeRule)
        {
            //从树结构编码规则中计算出父级分类的编码长度
            if (string.IsNullOrWhiteSpace(treeRule))
                return 0;
            var rules = treeRule.Split('-');
            if (rules.Length <= 1)
                return 0;

            var parentCodeLength = 0;
            for (int i = 0; i < rules.Length; i++)
            {
                var ruleValue = 0;
                int.TryParse(rules[i], out ruleValue);
                var currentCodeLength = parentCodeLength + ruleValue;

                if (currentCodeLength >= code.Length)
                    break;

                parentCodeLength = currentCodeLength;
            }

            return parentCodeLength;
        }
    }
}
